package com.wn.inventory.rabbitmq;

import com.rabbitmq.client.Channel;
import com.wn.entity.InventoryBookkeeping;
import com.wn.inventory.mapper.InventoryBookkeepingMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @ author     ：NST
 * @ date       ：Created in 2022/10/19 16:56
 * @ description：消息消费者
 * @ modified By：
 * @ version:
 */
@Component
public class Consumer {

    @Autowired
    InventoryBookkeepingMapper inventoryBookkeepingMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    /**
     * @param inventoryBookkeeping object obj,消息正文 发送的什么类型就要用什么类型接受
     * @param message              Message message, 消息对象 包含编码格式 头信息等等
     * @param channel              Channel channel, 信道
     * @return void
     * @ author nst
     * @ date 2022/10/20 19:44
     * @ Description 添加库存台账信息 并删除库存台账缓存
     */
    @RabbitListener(queues = "queueBookkeeping")
    public void receive(InventoryBookkeeping inventoryBookkeeping, Message message, Channel channel) {
        inventoryBookkeepingMapper.insert(inventoryBookkeeping);
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        if (Boolean.TRUE.equals(redisTemplate.hasKey("inventory"))) {
            hash.delete("inventory");
        }
    }
}
